package cn.jasonone.jfx.factory;

import cn.jasonone.jfx.manager.JfxFxmlManager;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xujie
 * @version 1.0
 * @date 2021/12/22 12:26
 */
@Component("sceneFactory")
public class DefaultSceneFactory implements SceneFactory {
    private Map<String, Scene> sceneMap = new HashMap<>();

    @Override
    public Scene getScene(String id) {
        Scene scene = sceneMap.get(id);
        if (scene == null) {
            scene = new Scene(new AnchorPane());
            sceneMap.put(id, scene);
        }
        return scene;
    }

    @Override
    public Scene getScene(Class controllerClass) {
        return getScene(controllerClass.getName());
    }

    @Override
    public Scene getScene(String id, double width, double height) {
        Scene scene = sceneMap.get(id);
        if (scene == null) {
            scene = new Scene(new AnchorPane(), width, height);
            sceneMap.put(id, scene);
        }
        return scene;
    }

    @Override
    public void destroy(Scene scene) {
        String id = null;
        for (Map.Entry<String, Scene> entry : sceneMap.entrySet()) {
            if(entry.getValue() == scene){
                id = entry.getKey();
            }
        }
        sceneMap.remove(id);
    }
}
